package Demo79;

import javax.xml.transform.SourceLocator;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Spliterator;

/**
 * 数据流中的第 K 大元素
 * https://leetcode.cn/problems/jBjn9C/
 */

class KthLargest {
    public PriorityQueue<Integer> queue;
    public Integer count = 0;
    public KthLargest(int k, int[] nums) {
        count = k;
        queue = new PriorityQueue<>();
        for(int n : nums) {
            queue.offer(n);
            if(queue.size() > count) queue.poll();
        }
    }
    
    public int add(int val) {
        queue.offer(val);
        if(queue.size() > count) queue.poll();
        return queue.peek();
    }
}
class Demo {
    public static void main(String[] args) {
        KthLargest kthLargest = new KthLargest(3, new int[] {4,5,8,2});
        System.out.println(Arrays.toString(kthLargest.queue.toArray()));
    }
}